// Shallow copy and deep copy problem

#include <iostream>
#include <cstring>
using namespace std;

class String {
	public:
		String( const char* psz="" ):m_psz( new char[strlen(psz)+1] ) {
			strcpy( m_psz, psz);
		}

		~String( ) {
			delete[] this->m_psz;
		}

		String( const String& that ):m_psz( new char[strlen(that.m_psz)+1]) {
			strcpy( m_psz, that.m_psz );
		}

		String& operator=( const String& that ) {
			if( this != &that ){
				delete[] this->m_psz;		// delete '/0'
				this->m_psz = new char[strlen(that.m_psz)+1];
				strcpy( this->m_psz, that.m_psz );
			}
			return *this;
		}

		char* c_str( ) { return m_psz; }
	private:
		char* m_psz;
};

// designer
// ------------------------------------------------------------------
// user

int main( void ) { 
	String s1( "hello" );
	cout << "s1 : " << s1.c_str() << endl;
	cout << "address : " << (void*)s1.c_str() << endl;

	String s2( s1 ) ;
	cout << "s2 : " << s1.c_str() << endl;
	cout << "address : " << (void*)s2.c_str() << endl;

	String s3;
	cout << "s3 : " << s3.c_str( ) << ", s3 prioter : " << (void*)s3.c_str( ) << endl;

	s3 = s2;
	cout << "s3 : " << s3.c_str( ) << ", s3 prioter : " << (void*)s3.c_str( ) << endl;


	return 0;
}

